import java.io.*;
import java.net.*;
/**class to controls all clients*/
public class ManagerOfClients {
    /** List of Clients */
	private Player [] clients;
	/** numeber of connected client */
	private int number;
	private Deck deck;
	private Game game;
	
	private int startingCoins;
	
	private int smallBlind;
	
	private int bigBlind;
	
	private boolean rejecting;
	
	private int rejectingPlayer;
	
	private boolean start;
	
	private int port;
	
	private int BotNumber;
	/**constructor.*/
	/**
	* @param startingCoins coins at the start
	* @param smallBlind smallBlind
	* @param bigBlind bigBlind
	* @param port server
	*/
	public ManagerOfClients(int startingCoins,int smallBlind,int bigBlind,int port)
	{
		this.port =port;
		start =false;
		this.rejecting =false;
		this.game =null;
		clients = new Player[6];
		int number = 0;
		this.startingCoins = startingCoins;
		this.smallBlind = smallBlind;
		this.bigBlind = bigBlind;
		this.rejectingPlayer = -1;
		this.BotNumber = 1;
	}
	/**Method adds new CLient*/
	public void addNewClient(Socket socket)
	{
		if(number < 6)
		{

		    for(int i = 0; i<6; i++)
		    {
		    		if(clients[i] == null)
		    		{
		    			try{
                           clients[i] = new Player(i, socket, this, startingCoins);
                           clients[i].start();
                           clients[i].send("Number_" + i);
                           if(i<5)
                           for(int j = i+1; i<6;i++)
                   		{
                   			if(this.clients[j]!=null)
                   			{
                   				this.clients[j].send("Number_"+(i+1));
                   			}
                   		}
                           
                           number++;
                           System.out.println(number);
		    			}
		    			catch(ClientException e)
		    			{
		    				ServerConnection tmpConnection = new ServerConnection(socket,this);
		    				tmpConnection.send("ERROR_" + e.getMessage());
		    			}
                           break;
		    		}
	        }
		}
		else
		{
			ServerConnection tmpConnection = new ServerConnection(socket,this);
			tmpConnection.send("ERROR_"+"Too many player in game");
		}

	}
	/** Method send data too all connected client */
	public void sendToAllClients( String data)
	{
		for( int i = 0; i<6; i++)
		{
			if(clients[i] != null)
			{
				clients[i].send(data);
			}
		}
	}
	/** Refresh clients list on client side. */
	public void refreshClientsList()
	{
		String data = new String();
		for(int i = 0; i<6; i++)
		{
			if(clients[i] != null)
			{
				data = data  +clients[i].getNick()+" ";

			}
		}
		
		this.sendToAllClients("ClientList_" + data);
	}
	
	public int numberOfPlayers()
	{
		return (number);
	}
	public int getStartingCoins()
	{
		return this.startingCoins;
	}
	public int getSmallBlind()
	{
		return this.smallBlind;
	}
	public int getBigBlind()
	{
		return this.bigBlind;
	}
	
	public Player[] getClients()
	{
		Player players[] = new Player[number];
		int j = 0;
		for(int i=0; i<6; i++)
		{
			if(this.clients[i] != null)
			{
				players[j] =this.clients[i];//sprawdz
				j++;
			}
		}
		return players;
	}
	
	public void startGame(int n)
	{
		int m = 0;
		for(int i =0; i<6; i++)
		{
			if(clients[i] != null)
				if(clients[i].getReady())
					m++;
				else
				   clients[i].send("Chat_ Players: "+clients[n].getNick() + " is ready to start");
		}
		if (m==number)
		{
			this.sendToAllClients("GameStart_");
			if(game == null)
				{
				deck = new Deck();
			
			    this.game = new Game(this, deck);
				}
			for(int i =0; i<6; i++)
			{
				if(clients[i] != null)
				{
					clients[i].startGame(game, deck);
				}
				
			}
			start =true;
			game.startGame();
		}
		else
		{
			clients[n].send("WaitForPlayers_");
		}
			
		}
	
	public void deleteClient(int n)
	{
		if(clients[n] != null)
			clients[n].kill();
		clients[n] = null;
        this.number =number-1;
		
		if(start)
		{
			game.deletePlayer(n);
			this.sendToAllClients("Fold_"+n);
		}
		else
		{
			
			Player[] tmpclients;
			tmpclients = this.getClients();
			for(int i=0; i<tmpclients.length; i++)
			{	System.out.println(i);
				this.clients[i] = tmpclients[i];
				clients[i].setNumber(i);
				this.clients[i].send("Number_"+i);
			
				
			}
			for(int i=tmpclients.length; i<6; i++)
			{	
				this.clients[i] = null;
			}
			this.refreshClientsList();
		}
	}

	//only if game is not started !!
	public void rejectPlayer(int rejected, int rejection)
	{
		if(!rejecting||game ==null)
		{rejectingPlayer = rejected;
		for(int i=0; i<6;i++)
		{
			if(i!=rejection&&i!=rejected&&clients[i]!=null)
				clients[i].rightToVote = true;
		}
		System.out.println(rejected);
		
		
		this.sendToAllClients("proposalToReject_" + clients[rejected].getNumber());
		this.reject(1);
		rejecting = true;
		
		}
		else
		{
			clients[rejected].send("Wait_");
		}
	}
	public void reject(int vote)
	{
		System.out.println("rejected" +rejectingPlayer );
		if(vote == 1)
		{System.out.println(vote);
			
			clients[rejectingPlayer].voteForRejection++;
			if(clients[rejectingPlayer].voteForRejection > number-2)
			{
				System.out.println("PlayerRejecting_" + clients[rejectingPlayer].getNumber());
				
				this.sendToAllClients("PlayerRejecting_" + clients[rejectingPlayer].getNumber());
				for(int i = rejectingPlayer; i<6;i++)
				{
					if(this.clients[i]!=null)
					{
						this.clients[i].send("Number_"+(i-1));
					}
				}
				deleteClient(rejectingPlayer);
				rejecting =false;
				rejectingPlayer =-1;
				for(int i=0; i<6;i++)
				{
					if(i!=rejectingPlayer&&clients[i]!=null)
						clients[i].rightToVote = false;
				}
			}
		}
		else
		{System.out.println(vote);
			System.out.println("PlayerNotRejecting_" + clients[rejectingPlayer].getNumber());
			this.sendToAllClients("PlayerNotRejecting_" + clients[rejectingPlayer].getNumber());
			rejecting = false;
			for(int i=0; i<6;i++)
			{
				if(i!=rejectingPlayer&&clients[i]!=null)
					clients[i].rightToVote = false;
			}
			clients[rejectingPlayer].voteForRejection = 0;
		}
		
	}
	public void addBot()
	{
        Runtime r=Runtime.getRuntime();
        try
        {
            r.exec("java Bot "+port+" localhost "+"Bot"+BotNumber);
        }
        catch(Exception e)
        {
            System.out.println(e.toString());
        }
        BotNumber++;
        
        if(BotNumber<0)
        	BotNumber = 1;
	}
public void stopGame()
{
	start =false;
	for(int i=0; i<6; i++)
	{
		if(clients[i]!= null)
		{
			clients[i].stopGame();
		}
	}
	sendToAllClients("Stop_");

	this.refreshClientsList();
	Player[] tmpclients;
	tmpclients = this.getClients();
	for(int i=0; i<tmpclients.length; i++)
	{	System.out.println(i);
		this.clients[i] = tmpclients[i];
		clients[i].setNumber(i);
		this.clients[i].send("Number_"+i);	
		
	}
	for(int i=tmpclients.length; i<6; i++)
	{	
		this.clients[i] = null;
	}
}
}
